/*
 * jETeL/CloverETL - Java based ETL application framework.
 * Copyright (c) Javlin, a.s. (info@cloveretl.com)
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.jetel.component;

import org.jetel.ctl.CTLAbstractTransform;
import org.jetel.ctl.CTLEntryPoint;
import org.jetel.ctl.TransformLangExecutorRuntimeException;
import org.jetel.data.DataRecord;
import org.jetel.exception.ComponentNotReadyException;
import org.jetel.exception.TransformException;

/**
 * Base class of all Java filter generated by CTL-to-Java compiler from CTL filters.
 *
 * @author Michal Tomcanyi, Javlin a.s. &lt;michal.tomcanyi@javlin.cz&gt;
 * @author Martin Janik, Javlin a.s. &lt;martin.janik@javlin.eu&gt;
 *
 * @version 11th June 2010
 * @created 28th April 2009
 *
 * @see RecordFilter
 */
public abstract class CTLRecordFilter extends CTLAbstractTransform implements RecordsFilter {

	/** Input data record used for filtering, or <code>null</code> if not accessible. */
	private DataRecord[] inputRecords = null;

	// this is just temporary array for #isValid(DataRecord record) to avoid create new array for each invocation
	private DataRecord[] tempInputRecords = new DataRecord[1];
	
	@Override
	public final void init() throws ComponentNotReadyException {
		globalScopeInit();
		initDelegate();
	}

	/**
	 * Called by {@link #init()} to perform user-specific initialization defined in the CTL transform. The default
	 * implementation does nothing, may be overridden by the generated transform class.
	 *
	 * @throws ComponentNotReadyException if the initialization fails
	 */
	@CTLEntryPoint(name = "init", required = false)
	protected void initDelegate() throws ComponentNotReadyException {
		// does nothing by default, may be overridden by generated transform classes
	}

	@Override
	public boolean isValid(DataRecord record) throws TransformException {
		tempInputRecords[0] = record;
		return isValid(tempInputRecords);
	}
	
	@Override
	public boolean isValid(DataRecord[] records) throws TransformException {
		boolean result = false;

		// only input record is accessible within the isValid() function
		inputRecords = records;

		try {
			result = isValidDelegate();
		} catch (ComponentNotReadyException exception) {
			// the exception may be thrown by lookups, sequences, etc.
			throw new TransformException("Generated transform class threw an exception!", exception);
		}

		// make the input record inaccessible again
		inputRecords = null;

		return result;
	}

	/**
	 * Called by {@link #isValid(DataRecord)} to perform user-specific filtering defined in the CTL transform.
	 * Has to be overridden by the generated transform class.
	 *
	 * @throws ComponentNotReadyException if some internal initialization failed
	 * @throws TransformException if an error occurred
	 */
	@CTLEntryPoint(name = "isValid", required = true)
	protected abstract Boolean isValidDelegate() throws ComponentNotReadyException, TransformException;

	@Override
	protected final DataRecord getInputRecord(int index) {
		if (inputRecords == null) {
			throw new TransformLangExecutorRuntimeException(INPUT_RECORDS_NOT_ACCESSIBLE);
		}

		if (index < 0 || index >= inputRecords.length) {
			throw new TransformLangExecutorRuntimeException(new Object[] { index }, INPUT_RECORD_NOT_DEFINED);
		}

		return inputRecords[index];
	}

	@Override
	protected final DataRecord getOutputRecord(int index) {
		throw new TransformLangExecutorRuntimeException(OUTPUT_RECORDS_NOT_ACCESSIBLE);
	}

}
